﻿using System.Windows;
using System.Windows.Controls;

using Gonte.Wpf.Controls.ViewModel;
using Gonte.Wpf.Controls.ViewModel.Commands;

using MyWpfControlsTestBench.Model;

namespace MyWpfControlsTestBench.DemoWindows
{
    /// <summary>
    /// Interaction logic for ObjectWithMultipleChildrenLoadOnDemandTypes.xaml
    /// </summary>
    public partial class ObjectWithMultipleChildrenLoadOnDemandTypes 
        : Window
    {
        public ObjectWithMultipleChildrenLoadOnDemandTypes()
        {
            InitializeComponent();

            // Create the model
            RootTreeItem2 rootItem = new RootTreeItem2();
            rootItem.Name = "Custom Root";

            // Extra initialization for the view model info
            ContentObjectViewModelInfo viewModelInfo = (ContentObjectViewModelInfo)ContentObjectViewModelInfo.GetInfo(typeof(RootTreeItem2));
            viewModelInfo.LoadOnDemand = true;

            ContentViewModelInfo folderViewModelInfo = viewModelInfo.GetFolderViewModelInfo("Children");
            folderViewModelInfo.ContextMenuFactory = delegate(object model)
            {
                ContextMenu menu = new ContextMenu();

                ItemViewModel.AddMenuItem(menu, "Add Child", null);

                return menu;
            };

            folderViewModelInfo.Label = "Folder for Children";
            folderViewModelInfo.CollapsedIconUri = @"pack://application:,,/Images/ClosedFolder.bmp";
            folderViewModelInfo.ExpandedIconUri = @"pack://application:,,/Images/OpenFolder.bmp";

            folderViewModelInfo = viewModelInfo.GetFolderViewModelInfo("Children2");
            folderViewModelInfo.ContextMenuFactory = delegate(object model)
            {
                ContextMenu menu = new ContextMenu();

                ItemViewModel.AddMenuItem(menu, "Add Child 2", null);

                return menu;
            };

            folderViewModelInfo.Label = "Folder for Children2";
            folderViewModelInfo.CollapsedIconUri = @"pack://application:,,/Images/ClosedFolder.bmp";
            folderViewModelInfo.ExpandedIconUri = @"pack://application:,,/Images/OpenFolder.bmp";

            // Create the view model
            ContentObjectViewModel viewModel = new ContentObjectViewModel(viewModelInfo, null, rootItem);

            // Set the callbacks of the folder view models to load the data
            FolderViewModel folderViewModel = viewModel.FindFolderViewModel("Children");
            folderViewModel.LoadData = delegate(object model)
            {
                for (int i = 0; i < 10; ++i)
                {
                    ChildTreeItem childItem = new ChildTreeItem();
                    childItem.Name = "Custom Child " + (i + 1).ToString();

                    rootItem.Children.Add(childItem);
                }
            };

            folderViewModel = viewModel.FindFolderViewModel("Children2");

            folderViewModel.LoadData = delegate(object model)
            {
                for (int i = 0; i < 10; ++i)
                {
                    ChildTreeItem2 childItem = new ChildTreeItem2();
                    childItem.Name = "Custom Child Type2 " + (i + 1).ToString();

                    rootItem.Children2.Add(childItem);
                }
            };

            viewModel.SelectionChanged += delegate(object sender, ViewModelEventArgs args)
            {
                MessageBox.Show(string.Format("Selected object: '{0}'", args.ViewModel.Model.ToString()));
            };

            viewModelInfo.ContextMenuFactory = delegate(object @object)
            {
                ContextMenu contextMenu = new ContextMenu();
                ViewModelBase.AddMenuItem(contextMenu, "Add a root", ViewModelCommands.AddChild);
                return contextMenu;
            };

            DataContext = demoTreeControl.ViewModel = viewModel;
        }
    }
}
